package com.atguigu.mapreduce.source.impl;

import com.atguigu.mapreduce.source.GeneralSource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class RelationSource extends GeneralSource {
    private final StringBuilder sb = new StringBuilder();
    Random rand = new Random();
    private final List<Integer> totalPeople;
    private int currentIndex = 0;

    public RelationSource() {
        totalPeople = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            totalPeople.add(i);
        }
    }

    @Override
    public String getLine() {
        if (currentIndex >= totalPeople.size()) {
            return "";
        }
        // 生成好友列表
        Collections.shuffle(totalPeople);
        List<Integer> friendList = totalPeople.subList(0, 1 + rand.nextInt(totalPeople.size() - 1));

        // 追加到文本
        sb.setLength(0);
        sb.append('P').append(String.format("%05d", currentIndex)).append(':');
        for (Integer friend : friendList) {
            if (friend == currentIndex) {
                continue;
            }
            sb.append('P').append(String.format("%05d", friend)).append(',');
        }
        currentIndex++;
        return sb.substring(0, sb.length() - 1);
    }
}
